package com.cskaoyan.service.keyword;

import com.cskaoyan.bean.Keyword;
import com.cskaoyan.bean.KeywordExample;
import com.cskaoyan.bean.ListData;
import com.cskaoyan.mapper.KeywordMapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 杨杰
 * @version V1.0
 * @Package com.cskaoyan.service
 * @date 2020/12/5 22:29
 * @Copyright © 暂时还没有
 */
@Service
public class KeywordServiceImp implements KeywordService{
    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public ListData<Keyword> keywordList
            (Integer page, Integer limit, String sort, String order, String keyword, String url){

        PageHelper.startPage(page,limit);
        KeywordExample keywordExample = new KeywordExample();
        keywordExample.setOrderByClause(sort + " " + order);
        KeywordExample.Criteria criteria = keywordExample.createCriteria();

        if (keyword != null){
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (url != null){
            criteria.andUrlEqualTo(url);
        }

        ListData<Keyword> listData = new ListData<>();
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);

        listData.setItems(keywords);
        PageInfo pageInfo = new PageInfo(keywords);
        listData.setTotal((int)pageInfo.getTotal());

        return listData;
    }

    @Override
    //public Keyword keywordCreate(Boolean isHot, Boolean isDefault, String keyword, String url) {
    public Keyword keywordCreate(Keyword keyword) {

        Keyword keywordNew = new Keyword();
        KeywordExample keywordExample = new KeywordExample();

        //判断参数是否正确
        if (keyword != null){
            keywordNew.setKeyword(keyword.getKeyword());
        }else throw new IllegalArgumentException("参数不能为null");
        if (keyword.getUrl() != null){
            keywordNew.setUrl(keyword.getUrl());
        }else throw new IllegalArgumentException("参数不能为null");

        //Boolean不为null设默认值
        if (keyword.getIsHot() == null){
            keywordNew.setIsHot(false);
        }else keywordNew.setIsHot(keyword.getIsHot());
        if (keyword.getIsDefault() == null){
            keywordNew.setIsDefault(false);
        }else keywordNew.setIsDefault(keyword.getIsDefault());

        //创建、修改日期
        Date parse = getCurrentTime();
        keywordNew.setAddTime(parse);//创建时间
        keywordNew.setUpdateTime(parse);//创建时间作为第一次修改时间

        keywordNew.setSortOrder(100);
        keywordNew.setDeleted(false);

        keywordMapper.insertSelective(keywordNew);//insert语句，selective

        return keywordNew;
    }

    @Override
    public void keywordDelete(Keyword keyword) {

        keywordMapper.deleteByPrimaryKey(keyword.getId());
    }

    @Override
    public Keyword keywordUpdate(Keyword keyword) {

        //查询修改后的记录并返回

        keyword.setUpdateTime(getCurrentTime());//纠正修改时间为当前时间

        keywordMapper.updateByPrimaryKey(keyword);
        Keyword keywordUpdate = keywordMapper.selectByPrimaryKey(keyword.getId());

        return keywordUpdate;

    }
    /*
    得到当前时间
     */
//    @DateTimeFormat(pattern = "yy-MM-dd")
    @JsonFormat(pattern = "yy-MM-dd HH:mm:ss",timezone = "GMT+8")
    static Date date = new Date();

    public static Date getCurrentTime(){

        long time = date.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");

        String stringTime = sdf.format(time);

        Date parse = null;
        try {
            parse = sdf.parse(stringTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }
}
